/**
 * Created by lenovo on 17-6-7.
 */
var express = require('express');
var router = express.Router();
var http = require('http');
var http_post = require('http-post');
var checkLogin = require('../middlewares/check').checkLogin;

router.get('/:user_id/',checkLogin,function(req,res,next){
    http.get('http://localhost:3000/get_user_note?user='+req.params.user_id,function(response){
        var rawData = '';
        response.setEncoding("utf-8");
        response.on("data", function(chunk){
            rawData += chunk;
        }).on('end',function(){
            res.render('my_note',{
                title:'myblog',
                note_infos: JSON.parse(rawData).data,
                user: req.session.user
            })
        })
    });
});

router.get('/get_note/:user_id/:note_id',checkLogin,function(req,res,next){//获得一篇帖子
    http.get('http://localhost:3000/get_note?note_id='+req.params.note_id,function(response){
        var rawData = '';
        response.setEncoding("utf-8");
        response.on("data", function(chunk){
            rawData += chunk;
        }).on('end',function(){
            res.render('get_note',{
                title:'myblog',
                note_info: JSON.parse(rawData).data[0],
                user: req.session.user
            })
        })
    });
});

router.get('/create/:user_id',checkLogin,function(req,res,next){
    res.render('create_note',{
        title:'myblog',
        user: req.session.user
    })
});

router.post('/create/:user_id',checkLogin,function(req,res,next){
    http_post('http://localhost:3000/create_note?user='+req.params.user_id,req.body,function(response){
        var rawData = '';
        response.setEncoding('utf8');
        response.on('data',function(chunk){
            rawData += chunk;
        }).on("end",function(){
            if(JSON.parse(rawData).status == 200){
                res.json(JSON.parse(rawData))
            }else{
                res.json(JSON.parse(rawData))
            }
        })
    }).on("error",function(e){
        console.error(e)
    });
});

router.get('/update/:user_id',checkLogin,function(req,res,next){
    http.get('http://localhost:3000/get_note?note_id='+req.query._id,function(response){
        var rawData = '';
        response.setEncoding("utf-8");
        response.on("data", function(chunk){
            rawData += chunk;
        }).on('end',function(){
            res.render('update_note',{
                title:'myblog',
                note_info: JSON.parse(rawData).data[0],
                user: req.session.user
            })
        })
    });
});

router.post('/update/:user_id',checkLogin,function(req,res,next){
    http_post('http://localhost:3000/update_note?user='+req.params.user_id,req.body,function(response){
        var rawData = '';
        response.setEncoding('utf8');
        response.on('data',function(chunk){
            rawData += chunk;
        }).on("end",function(){
            if(JSON.parse(rawData).status == 200){
                res.json(JSON.parse(rawData))
            }else{
                res.json(JSON.parse(rawData))
            }
        })
    }).on("error",function(e){
        console.error(e)
    });
});

router.get('/delete/:user_id',checkLogin,function(req,res,next){
    http.get('http://localhost:3000/delete_note?_id='+req.query._id,function(response){
        var rawData = '';
        response.setEncoding("utf-8");
        response.on("data", function(chunk){
            rawData += chunk;
        }).on('end',function(){
            res.json({status:200,message:'删除帖子成功'})
        })
    });
});

router.post('/comment',function(req,res,next){
    console.log(req.body);
    http_post('http://localhost:3000/add_comment',req.body,function(response){
        var rawData = '';
        response.setEncoding('utf8');
        response.on('data',function(chunk){
            rawData += chunk;
        }).on("end",function(){
            if(JSON.parse(rawData).status == 200){
                res.json(JSON.parse(rawData))
            }else{
                res.json(JSON.parse(rawData))
            }
        })
    }).on("error",function(e){
        console.error(e)
    });
});

router.post('/del_comment',function(req,res,next){
    http.get('http://localhost:3000/del_comment?_id='+req.body.comment_id + '&note_id=' + req.body.note_id,function(response){
        var rawData = '';
        response.setEncoding("utf-8");
        response.on("data", function(chunk){
            rawData += chunk;
        }).on('end',function(){
            res.json(JSON.parse(rawData))
        })
    });
});

module.exports = router;